<?php ;
require mnminclude.'class.BasePage.inc';

class SetThemePage extends BasePage {
    public $center = 'set_theme';
    public $title = 'Change Theme';

    private $changed = false;

    public function process() {
        parent::process();

        if (isset($_GET['theme'])) {
            $theme = $_GET['theme'];
            try {
                Themes::validate($theme);
                Themes::setCookie($theme);
                $this->changed = true;
            } catch (Exception $e) {
                $this->error($e->getMessage());
            }
        }
    }

    public function render() {
        if ($this->changed) {
            exit(header('Location: ' . $this->selfUrl()));
        }

        $smarty = $this->smarty;

        $current = Themes::get();
        $themes = array();
        foreach (Themes::$data['themes'] as $name => $info) {
            $info['current'] = $name == $current;
            $info['name'] = $name;
            $info['url'] = $this->selfUrl() . '?theme=' . $name;
            $themes[] = $info;
        }
        $smarty->assign('themes', $themes);

        parent::render();
    }
}
